#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
extern char** environ;
int main(int argc,char* argv[],char* env[])
{
  //main函数最多三个参数，是父进程传递给我们的
  (void)argc;
  (void)argv;
  if(fork()==0)
  {
    for(int i = 0;environ[i];i++)
    {
      printf("environ[%d]->%s\n",i,environ[i]);
    }
  }
  sleep(3);
  const char* who = getenv("USER");
  if(who == nullptr) return 1;
  if(strcmp(who,"who")==0)
  {
    printf("这是程序正常运行逻辑");
  }
  else{
    printf("only root");
  }
  char* value = getenv("path");
  if(value == nullptr)
  {
     return 1;
  }
  printf("path->%s\n",value);
  for(int i = 0;env[i];i++)
  {
    printf("%d->%s",i,env[i]);
  }
  if(argc != 2)
  {
    printf("Usage:%s[-a][-b][-c]",argv[0]);
    return 1;
  }
  const char* arg = argv[1];
  if(strcmp(arg,"-a") == 0)
  {
      printf("这是功能1");
  }
  else if(strcmp(arg,"-b") == 0)
  {
    printf("这是功能2");
  }
  else if(strcmp(arg,"-c") == 0)
  {
    printf("这是功能3");
  }
  else
  {
    printf("usage:%s [-a][-b][-c]",argv[0]);
  }
  for(int i = 0 ;i< argc;i++)
  {
    printf("argv[%d]:%s",i,argv[i]);
  }
  return 0;
}
